import { getEntireRoomListData } from "@/services/modules/entire"
import * as ActionTypes from "./constants"

export const changeCurrentPageAction = (currentPage) => ({
  type: ActionTypes.CHANGE_CURRENT_PAGE,
  currentPage,
})

export const changeRoomListAction = (roomList) => ({
  type: ActionTypes.CHANGE_ROOM_LIST,
  roomList,
})

export const changePageTotalAction = (pageTotal) => ({
  type: ActionTypes.CHANGE_PAGE_TOTAL,
  pageTotal,
})

export const changeIsloadingAction = (isLoading) => ({
  type: ActionTypes.CHANGE_ISLOADING,
  isLoading,
})

// 要给 page 一个初始值
export const fetchRoomListAction = (page = 0) => {
  // 返回一个新的函数，里面有参数 dispatch getState
  return async (dispatch, getState) => {
    dispatch(changeCurrentPageAction(page))
    // 动态获取当前页码值,每次 与偏移量相乘
    // const page = getState().entire.currentPage
    dispatch(changeIsloadingAction(true))
    const res = await getEntireRoomListData(page * 20)
    dispatch(changeIsloadingAction(false))
    // 将数据保持到 redux
    const total = res.totalCount
    const roomList = res.list
    dispatch(changePageTotalAction(total))
    dispatch(changeRoomListAction(roomList))
  }
}
